package com.qiaofang.housewarehouse.service.util;

import com.google.common.collect.Lists;
import org.springframework.cglib.beans.BeanMap;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/31.
 */
public class Utils {

    public static <T> List<T> mapsToObjects(List<Map<String, Object>> maps, Class<T> clazz) throws InstantiationException, IllegalAccessException {
        List<T> list = Lists.newArrayList();
        try{
           if (maps != null && maps.size() > 0) {
               Map<String, Object> map = null;
               T bean = null;
               for (int i = 0,size = maps.size(); i < size; i++) {
                   map = maps.get(i);
                   bean = clazz.newInstance();
                   mapToBean(map, bean);
                   list.add(bean);
               }
           }
       }catch (Exception e){
            e.printStackTrace();
       }
       return list;
    }

    public static <T> T mapToBean(Map<String, Object> map,T bean) {
        try {
            BeanMap beanMap = BeanMap.create(bean);
            beanMap.putAll(map);
        }catch (Exception e){
            e.printStackTrace();
        }
        return bean;
    }
}
